//
//  SNHealthySelectTypeView.m
//  project
//
//  Created by 黄建华 on 2017/12/27.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNHealthySelectTypeView.h"

@implementation SNHealthySelectTypeView {
    UIView *line;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        NSMutableArray *array = [NSMutableArray array];
        for (int i = 0; i < SN_PublicClass.function.count; i++) {
            NSString *value = [SN_PublicClass.function objectAtIndex:i];
            if ([value isEqualToString:@"2"]) {
                [array addObject:SN_Text(@"心率")];
            }
            if ([value isEqualToString:@"3"]) {
                [array addObject:SN_Text(@"血氧")];
            }
            if ([value isEqualToString:@"4"]) {
                [array addObject:SN_Text(@"血压")];
            }
        }
        
        for (int i = 0; i < array.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = SNFRAME(HHHWIDTH / array.count *i, 0, HHHWIDTH / array.count, self.height);
            [button addTarget:self
                       action:@selector(buttonclick:)
             forControlEvents:UIControlEventTouchUpInside];
            button.tag = i +2;
            button.titleLabel.adjustsFontSizeToFitWidth = YES;
            button.titleLabel.minimumScaleFactor = 0.5;
            [button setTitleColor:SNCOLORGRAY forState:UIControlStateNormal];
            [button setTitleColor:SNCOLORBLACK forState:UIControlStateSelected];
            [button setTitle:[array objectAtIndex:i] forState:UIControlStateNormal];
            [self addSubview:button];
            if (i == 0) {
                button.selected = YES;
            } else {
                button.selected = NO;
            }
        }
        if (SN_PublicClass.function.count == 3) {
            line = [[UIView alloc]initWithFrame:CGRectMake(HHHWIDTH/4 +10, self.height-2, HHHWIDTH/2 -20, 2)];
        } else {
            line = [[UIView alloc]initWithFrame:CGRectMake(HHHWIDTH/array.count*0.15, self.height-2, HHHWIDTH/array.count*0.7, 2)];
        }
        
        line.backgroundColor = SNCOLORBLACK;
        [self addSubview:line];
        
        _isCheck = NO;
    }
    return self;
}

- (void)buttonclick:(UIButton *)sender {
    if (_isCheck) {
        SHOWMBProgressHUD(nil,SN_Text(@"正在体检"), nil, NO, 2.0);
        return;
    }
    CheckType type = CheckHeart;
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            ((UIButton *)view).selected = NO;
        }
    }
    NSString *value = [SN_PublicClass.function objectAtIndex:sender.tag];
    if (value.intValue == 2) {
        type            = CheckHeart;
        sender.selected = YES;
    }
    if (value.intValue == 3) {
        type            = CheckOX;
        sender.selected = YES;
    }
    if (value.intValue == 4) {
        type            = CheckBP;
        sender.selected = YES;
    }
    
    if (SN_PublicClass.function.count == 3) {
        line.frame = CGRectMake(HHHWIDTH/4 +10, self.height-2, HHHWIDTH/2 -20, 2);
    } else {
        line.frame = CGRectMake(HHHWIDTH/(SN_PublicClass.function.count-2)*0.15+HHHWIDTH/(SN_PublicClass.function.count-2)*(sender.tag-2), self.height-2, HHHWIDTH/(SN_PublicClass.function.count-2)*0.7, 2);
    }
        if (_selectType) {
    }
    if (_selectType) {
        _selectType(type);
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end

